<template>
  <div >
    <van-form ref="form"  @submit="onSubmit" @failed="onfailed">
      <van-field
        v-model="obj.phone"
        name="phone"
        label="手机号码"
        placeholder="手机号码"
        clearable
         left-icon="phone"
        :rules="[
          { required: true, message: '请填写手机号码' },
          {
            pattern:reg.phone,
            message:'请输入正确的手机号码'
          }
        ]"
      />
     <van-field
        v-model="obj.password"
        name="password"
        label="登录密码"
        placeholder="登录密码"
        clearable
         left-icon="lock"
        :type="type"
         :right-icon="type=='password'?'eye':'closed-eye'"
         @click-right-icon="changeType"
        :rules="[
          { required: true, message: '请填写登录密码' },
          {
            validator:(val)=>{
              return reg.pwd.test(val)
            },
            message:'请输入6-16位的数字字母组合的密码'
          }
        ]"
      />
      <div class="mt10 flex jcsb">
         <span class="c-main" @click="gotoPage('register')" >没有账号,立即注册?</span>
         <span  class="c-green" @click="gotoPage('forgetpass')" >忘记密码</span>
      </div>
      <div class="mt10">
        <van-button  v-shuibowen block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data(){
    return {
      type:"password",
      obj:{

      }
    }
  },
  mounted(){
    if(this.$route.query.phone){
      this.obj = {
        ...this.obj,
        phone:this.$route.query.phone
      }
    }else if(localStorage.phone){
      this.obj = {
        ...this.obj,
        phone:localStorage.phone
      }
    }
  },
  methods:{
    onfailed(){
      this.showerror("请先完善登录信息")
    },
    changeType(){
      this.type = this.type=='password'?'text':'password'
    },
    onSubmit(value){
       this.$http.finduser(value)
       .then(res=>{
         if(res.length>0){
            // 登录成功  
            // 1. 注册 + 忘记密码  返回    进入 个人中心   其他 go(-1)
            // 2. 存储手机号码 
            this.$notify({type:"success",message:"登录成功"})
            localStorage.setItem("phone",value.phone);   // 判断是否登录
            var fromName = localStorage.fromName;


            if(fromName=='register' || fromName =='forgetpass'){
              this.$router.push({name:'mine'})
            }else{
              this.$router.go(-1);
            }


            
         }else{
            this.showerror("登录失败")
         }
       })
    }
  }
}
</script>

